package com.example.springboot2.Control;

import com.alibaba.fastjson.JSONObject;
import com.example.springboot2.CommonHelper.RedisHelper;
import com.example.springboot2.DB_Helper.DataBase;
import com.example.springboot2.DB_Helper.JDBCUtils;
import com.example.springboot2.Model.Sys_Module;
import com.example.springboot2.Model.Sys_User;
import com.example.springboot2.Service.Sys_UserService;
import com.example.springboot2.myannotation.NoAuth;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/Sys_User")
public class Sys_UserController {
    private Sys_UserService sys_userService=new Sys_UserService();
    private DataBase<Sys_User> database=new DataBase<Sys_User>();
    @Resource
    private RedisHelper redisHelper=new RedisHelper();
    @PostMapping("/Login")
    @NoAuth
    public ReturnObject Login(@RequestParam()String UserName, @RequestParam() String UserPassWord)
    {
        ReturnObject result=new ReturnObject();
        result.Code=200;
        if(UserName.isEmpty())
        {
            result.Code=201;
            result.Result="用户名不能为空";
            return result;
        }
        List<Sys_User> UserList=sys_userService.GetEntityByUserCode(UserName);
        if(UserList==null||UserList.size()==0)
        {
            result.Code=202;
            result.Result="用户名错误，没有该用户";
            return result;
        }
        Sys_User user = UserList.get(0);
        if(user.Sys_UserPassword.equals(UserPassWord))
        {
            result.Code=200;
            user.token = UUID.randomUUID().toString();
            result.Result=user;

            //将token写入redis
            redisHelper.set(user.token,user);
        }
        else
        {
            result.Code=203;
            result.Result="密码错误";
        }
        return result;
    }
    @PostMapping("/GetPageList")
    public ReturnListTable GetPageList(@RequestParam int limit,@RequestParam int page)
    {
        ReturnListTable result=new ReturnListTable();
        result.code=0;
        result.data=sys_userService.GetPageList(limit,page);
        result.count=sys_userService.GetCount();
        return result;
    }
    @PostMapping("/SaveUser")
    public  ReturnString SaveUser(@RequestParam()String KeyValue,@RequestParam()String StrEntity) throws IllegalAccessException {
        ReturnString result=new ReturnString();
        result.Code=200;
        Sys_User user=  JSONObject.parseObject(StrEntity,Sys_User.class);
        String sql="";
        if(KeyValue.isEmpty())
        {
            user.Sys_UserId= UUID.randomUUID().toString();
            sql=database.add(user);
        }
        else
        {
            sql=database.update(user);
        }
        if(!user.roleIds.isEmpty())
        {
            sql=sql+" delete from Sys_UserRole where Sys_UserId='"+user.Sys_UserId+"' ";
            String[] roleArr = user.roleIds.split(",");
            for (String roleId:
                    roleArr) {
                sql=sql+" insert into Sys_UserRole values(newid(),'"+user.Sys_UserId+"','"+roleId+"') ";
            }
        }
        Connection conn = JDBCUtils.findConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        try {
            pre = conn.prepareStatement(sql);
            pre.execute();
            result.Message="保存成功";
            return  result;

        } catch (SQLException e) {
            result.Message=e.toString();
            //e.printStackTrace();
        }
        return result;
    }
}
